package com.tico.translatereditor.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.common.webResponse.WebResponse;
import com.tico.translatereditor.domain.KeyValuePair;
import com.tico.translatereditor.domain.MessageModel;
import com.tico.translatereditor.domain.TranslaterData;
import com.tico.translatereditor.param.TranslaterDataParam;
import com.tico.translatereditor.service.KeyValuePairService;
import com.tico.translatereditor.service.TranslaterDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/translater")
@Slf4j
public class TranslaterController {

    @Autowired
    private TranslaterDataService translaterDataService;
    @Autowired
    private KeyValuePairService keyValuePairService;
    @PostMapping(value = "/test")
    public WebResponse test(@RequestBody TranslaterData translaterData, HttpServletRequest request) {
        WebResponse wr = new WebResponse();
        String content="";
        try {
            content = keyValuePairService.translaterMsg(translaterData);
        } catch (Exception e) {
            e.printStackTrace();
            wr.setSuccess(false);
            wr.setErr_msg("失败");
        }
        wr.setData(content);
        wr.setSuccess(true);
        wr.setErr_msg("成功");
        return wr;
    }
    @PostMapping(value = "/save")
    public WebResponse save(@RequestBody TranslaterData translaterData, HttpServletRequest request) {
        WebResponse wr = new WebResponse();
        try {
            translaterData.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            translaterDataService.insert(translaterData);
        } catch (Exception e) {
            e.printStackTrace();
            wr.setSuccess(false);
            wr.setErr_msg("失败");
        }
        wr.setData(translaterData);
        wr.setSuccess(true);
        return wr;
    }
    @GetMapping(value = "/list")
    public JSONObject list(TranslaterDataParam translaterDataParam) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(translaterDataParam.getPage());
            baseQuery.setLimit(translaterDataParam.getLimit());
            QueryResult<TranslaterData> pageInfo = translaterDataService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(TranslaterData.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody TranslaterDataParam translaterDataParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = translaterDataParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                translaterDataService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }

    @PostMapping(value = "/returnDataCheck")
    public JSONObject returnDataCheck(@RequestBody TranslaterData translaterData, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = translaterData.getIds().split(",");
            for(int i=0;i<str.length;i++){
                translaterData.setId(str[i]);
                translaterDataService.update(translaterData);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
